﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

class Menu
{
    private int x = 0;
    private int y = 0;

    public void drawMenu()
    {
        JustBlackJackGame.RemoveScrollBar();


        string startGame = " [S]tart Game ";
        string rules = "    [R]ules   ";
        string controls = "How to [P]lay?";
        string exitGame = "  [E]xit Game ";

        x = Console.WindowWidth / 2 - 7;
        y = Console.WindowHeight / 2 + 1;

        while (true)
        {
            Console.BackgroundColor = ConsoleColor.DarkGreen; //Set background color
            Console.Clear();
            bool isKeyValid = false;

            Console.ForegroundColor = ConsoleColor.White;
            Logo.DrawLogo();
            Console.BackgroundColor = ConsoleColor.Red;

            DrawElements(x, y, startGame);
            DrawElements(x, y + 1, rules);
            DrawElements(x, y + 2, controls);
            DrawElements(x, y + 3, exitGame);

            do
            {
                ConsoleKeyInfo result = Console.ReadKey(true);
                if (result.Key == ConsoleKey.S)
                {
                    return;
                }

                if (result.Key == ConsoleKey.R)
                {
                    Console.BackgroundColor = ConsoleColor.DarkGreen; //Set background color
                    Console.Clear();
                    Rulesss();
                    isKeyValid = true;
                }

                if (result.Key == ConsoleKey.P)
                {
                    Console.BackgroundColor = ConsoleColor.DarkGreen; //Set background color
                    Console.Clear();
                    HowPlay();
                    isKeyValid = true;
                }

                if (result.Key == ConsoleKey.E)
                {
                    Environment.Exit(0);
                }
            } while (!isKeyValid);
        }
    }

    private void DrawElements(int x, int y, string text)
    {
        Console.SetCursorPosition(x, y);
        Console.Write(text);
    }

    private void Rulesss()
    {
        try
        {
            StreamReader reader = new StreamReader("../../rules.txt");
            using (reader)
            {
                string line = reader.ReadLine();
                while (line != null)
                {
                    Console.WriteLine("{0}", line);
                    line = reader.ReadLine();
                }
            }
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("Press any key to go back....");
            Console.ReadKey(true);
            Console.Clear();
        }
        catch (ArgumentNullException)
        {
            Console.WriteLine("Path is null.");
        }
        catch (ArgumentException)
        {
            Console.WriteLine("Path is an empty string");
        }

        catch (FileNotFoundException)
        {
            Console.WriteLine("The file cannot be found.");
        }
        catch (DirectoryNotFoundException)
        {
            Console.WriteLine("The specified path is invalid, such as being on an unmapped drive.");
        }
        catch (IOException)
        {
            Console.WriteLine("Path includes an incorrect or invalid syntax for file name, directory name, or volume label.");
        }
    }

    private void HowPlay()
    {
        try
        {
            StreamReader reader = new StreamReader("../../play.txt");
            using (reader)
            {
                string line = reader.ReadLine();
                while (line != null)
                {
                    Console.WriteLine("{0}", line);
                    line = reader.ReadLine();
                }
            }
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("Press any key to go back....");
            Console.ReadKey(true);
            Console.Clear();
        }
        catch (ArgumentNullException)
        {
            Console.WriteLine("Path is null.");
        }
        catch (ArgumentException)
        {
            Console.WriteLine("Path is an empty string");
        }

        catch (FileNotFoundException)
        {
            Console.WriteLine("The file cannot be found.");
        }
        catch (DirectoryNotFoundException)
        {
            Console.WriteLine("The specified path is invalid, such as being on an unmapped drive.");
        }
        catch (IOException)
        {
            Console.WriteLine("Path includes an incorrect or invalid syntax for file name, directory name, or volume label.");
        }
    }
}